package org.afun.csms_android.adapter;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import org.afun.csms_android.R;
import org.afun.csms_android.domain.Course;
import org.afun.csms_android.utils.MyTableOperator;
import org.afun.csms_android.utils.TableOperator;

import java.util.List;

public class AuditCourseAdapter extends RecyclerView.Adapter<AuditCourseAdapter.ViewHolder> {
    private List<Course> mCourseList;
    private Context mContext;

    static class ViewHolder extends RecyclerView.ViewHolder {
        View AuditView;
        ImageView courseImage;
        TextView courseMsg;
        Button auditPassBtn;
        Button auditRefuseBtn;

        public ViewHolder(View view) {
            super(view);
            AuditView = view;
            courseImage = (ImageView) view.findViewById(R.id.auditcourse_image);
            courseMsg = view.findViewById(R.id.auditcourse_msg);
            auditPassBtn = view.findViewById(R.id.auditPassBtn);
            auditRefuseBtn = view.findViewById(R.id.auditRefuseBtn);
        }

    }

    public AuditCourseAdapter(List<Course> courseList, Context context) {
        this.mCourseList = courseList;
        mContext = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.auditcourse_item, parent, false);
        final ViewHolder holder = new ViewHolder(view);

        // 禁止复用，因为查出来的有些是带审核按钮有些不带的
        holder.setIsRecyclable(false);

        holder.AuditView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                Course course = mCourseList.get(position);
//                Toast.makeText(v.getContext(), "you clicked view " + course.getCouId(), Toast.LENGTH_SHORT).show();
            }
        });
        holder.courseImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                Course course = mCourseList.get(position);
//                Toast.makeText(v.getContext(), "you clicked image " + course.getCouId(), Toast.LENGTH_SHORT).show();
            }
        });

        holder.auditPassBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                Course course = mCourseList.get(position);
                new AlertDialog.Builder(mContext).setTitle("提示")
                        .setMessage("你是否确定审核通过为" + course.getCouId() + "的课程？")
                        .setPositiveButton("是", new DialogInterface.OnClickListener() {
                            @SuppressLint("NotifyDataSetChanged")
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                course.setIsAudit(2);
                                MyTableOperator myTableOperator = new MyTableOperator(mContext);
                                TableOperator tableOperator = myTableOperator.getTableOperator();
                                boolean b = tableOperator.auditCourse(course);
                                if (b) {
                                    new androidx.appcompat.app.AlertDialog.Builder(mContext)
                                            .setTitle("提示信息")
                                            .setMessage("审核通过！")
                                            .setPositiveButton("确定", null)
                                            .create()
                                            .show();
                                    notifyDataSetChanged();
                                } else {
                                    new androidx.appcompat.app.AlertDialog.Builder(mContext)
                                            .setTitle("提示信息")
                                            .setMessage("审核异常...")
                                            .setPositiveButton("确定", null)
                                            .create()
                                            .show();
                                }

                            }
                        })
                        .setNegativeButton("否", null)
                        .show();
            }
        });
        holder.auditRefuseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                Course course = mCourseList.get(position);
                new AlertDialog.Builder(mContext).setTitle("提示")
                        .setMessage("你是否确定审核拒绝为" + course.getCouId() + "的课程？")
                        .setPositiveButton("是", new DialogInterface.OnClickListener() {
                            @SuppressLint("NotifyDataSetChanged")
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                course.setIsAudit(0);
                                MyTableOperator myTableOperator = new MyTableOperator(mContext);
                                TableOperator tableOperator = myTableOperator.getTableOperator();
                                boolean b = tableOperator.auditCourse(course);
                                if (b) {
                                    new androidx.appcompat.app.AlertDialog.Builder(mContext)
                                            .setTitle("提示信息")
                                            .setMessage("审核拒绝成功！")
                                            .setPositiveButton("确定", null)
                                            .create()
                                            .show();
                                    notifyDataSetChanged();
                                } else {
                                    new androidx.appcompat.app.AlertDialog.Builder(mContext)
                                            .setTitle("提示信息")
                                            .setMessage("审核异常...")
                                            .setPositiveButton("确定", null)
                                            .create()
                                            .show();
                                }

                            }
                        })
                        .setNegativeButton("否", null)
                        .show();
            }
        });

        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Course course = mCourseList.get(position);
        holder.courseImage.setImageResource(R.drawable.img);
        int isAudit = course.getIsAudit();
        String auditMsg = "";
        switch (isAudit) {
            case 0:
                auditMsg = "审核未通过";
                break;
            case 1:
                auditMsg = "审核中";
                break;
            case 2:
                auditMsg = "审核通过";
                break;
            default:
                break;
        }

        holder.courseMsg.setText(
                "课程号：" + course.getCouId() + " \n课程名：" + course.getCouName() +
                        "\n学分：" + course.getCredit() + "\n审核状态：" + auditMsg);

        if (isAudit != 1) {
            holder.auditRefuseBtn.setVisibility(View.GONE);
            holder.auditPassBtn.setVisibility(View.GONE);
        }
    }


    @Override
    public int getItemCount() {
        return mCourseList.size();
    }
}
